package com.v1.video.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.iss.view.pulltorefresh.PullToRefreshBase;
import com.iss.view.pulltorefresh.PullToRefreshBase.Mode;
import com.iss.view.pulltorefresh.PullToRefreshBase.OnRefreshListener2;
import com.iss.view.pulltorefresh.PullToRefreshListView;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.v1.video.Constant;
import com.v1.video.R;
import com.v1.video.adapter.FriendDynamicsAdapter;
import com.v1.video.domain.FriendDynamicsEntity;
import com.v1.video.domain.LoginInfo;
import com.v1.video.httpmanager.RequestManager;
import com.v1.video.httpmanager.RequestManager.OnResponseListener;
import com.v1.video.util.Helper;
import com.v1.video.util.Logger;

/**
 * 好友动态
 * 
 * @author wangfang 2016.4.13
 */
public class FriendDynamicsActivity extends BaseActivity implements
		OnClickListener {

	private static final String TAG = "FriendDynamicsActivity";
	private View mEmptyView;
	private PullToRefreshListView mRefreshListView;
	private ListView mListView;
	private TextView mTvTitle;
	private FriendDynamicsAdapter mAdapter;
	private ImageView mIvRight;
	private int mCurIndex = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_frienddynamics);
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	@Override
	protected void initView() {
		// TODO Auto-generated method stub
		mEmptyView = getLayoutInflater().inflate(
				R.layout.fragment_no_content, null);
		mTvTitle = (TextView) this.findViewById(R.id.tv_title);
		mIvRight = (ImageView) this.findViewById(R.id.iv_right);
		mRefreshListView = (PullToRefreshListView) this
				.findViewById(R.id.lv_listView);
		mRefreshListView.setScrollingWhileRefreshingEnabled(false);
		mRefreshListView.setMode(Mode.PULL_FROM_START);
		mListView = mRefreshListView.getRefreshableView();
	}

	@Override
	protected void initData() {
		// TODO Auto-generated method stub
		mTvTitle.setText(this.getResources().getString(
				R.string.mainpage_my_friend_msg));
		mIvRight.setImageResource(R.drawable.frienddynamics_addfriend_selecter);
		mIvRight.setVisibility(View.VISIBLE);
		mAdapter = new FriendDynamicsAdapter(this);
		mListView.setAdapter(mAdapter);
		getServerData();
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	protected void setListener() {
		// TODO Auto-generated method stub
		this.findViewById(R.id.lay_result).setOnClickListener(this);
		mIvRight.setOnClickListener(this);
		mEmptyView.setOnClickListener(this);
		mRefreshListView.setOnRefreshListener(new OnRefreshListener2() {

			@Override
			public void onPullDownToRefresh(PullToRefreshBase refreshView) {
				// TODO Auto-generated method stub
				mCurIndex = 0;
				if (Helper.checkConnection(FriendDynamicsActivity.this)) {
					getServerData();
				} else {
					if (mRefreshListView != null) {
						mRefreshListView.onRefreshComplete();
					}
					showToast(FriendDynamicsActivity.this.getResources()
							.getString(R.string.net_no_notclick));
				}
			}

			@Override
			public void onPullUpToRefresh(PullToRefreshBase refreshView) {
				// TODO Auto-generated method stub
				if (Helper.checkConnection(FriendDynamicsActivity.this)) {
					getServerData();
				} else {
					if (mRefreshListView != null) {
						mRefreshListView.onRefreshComplete();
					}
					showToast(FriendDynamicsActivity.this.getResources()
							.getString(R.string.net_no_notclick));
				}
			}
		});

		mListView.setOnScrollListener(new OnScrollListener() {

			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				// TODO Auto-generated method stub
				switch (scrollState) {
				case OnScrollListener.SCROLL_STATE_IDLE:
					ImageLoader.getInstance().resume();
					break;
				case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
					ImageLoader.getInstance().pause();
					break;
				case OnScrollListener.SCROLL_STATE_FLING:
					ImageLoader.getInstance().pause();
					break;
				}
			}

			@Override
			public void onScroll(AbsListView view, int firstVisibleItem,
					int visibleItemCount, int totalItemCount) {
				// TODO Auto-generated method stub

			}
		});
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.lay_result:
			this.finish();
			break;
		case R.id.iv_right:
			Intent intent = new Intent();
			intent.setClass(this, AddFriendActivity.class);
			startActivity(intent);
			break;
		case R.layout.fragment_no_content:
			if (mRefreshListView != null) {
				mRefreshListView.doPullRefreshing(100);
			}
			break;
		default:
			break;
		}
	}

	/**
	 * 设置空的界面显示
	 */
	private void setEmptyView() {
		if (mCurIndex == 0) {
			View view = mListView.getEmptyView();
			if (view == null) {
				mListView.setEmptyView(mEmptyView);
				TextView tvContent = (TextView) mEmptyView
						.findViewById(R.id.fragment_page_no_content);
				tvContent.setText("暂无更新,关注后更精彩");
			}
			if (mRefreshListView != null) {
				mRefreshListView.setMode(Mode.PULL_FROM_START);
			}
		}
	}

	/**
	 * 获取服务器数据
	 */
	private void getServerData() {
		String url = String.format(Constant.FRIENDDYNAMICS_LIST, LoginInfo
				.getInstance().getUserId(), mCurIndex,
				Constant.HOME_WATERFULL_PAGE_SIZE, Constant.PRODUCT_CODE,
				Constant.PRODUCT_VERSION, Constant.DEVICEID);
		Logger.i(TAG, "好友动态url==" + url);
		RequestManager.getInstance().getRequest(this, url,
				FriendDynamicsEntity.class, new OnResponseListener() {

					@Override
					public void onSuccess(Object object, int code,
							String message, String keyId) {
						// TODO Auto-generated method stub
						if (mRefreshListView != null) {
							mRefreshListView.onRefreshComplete();
						}
						if (object != null) {
							FriendDynamicsEntity entity = (FriendDynamicsEntity) object;
							if (entity != null && entity.getBody() != null
									&& entity.getBody().getData() != null) {
								if (mAdapter != null) {
									if (mCurIndex == 0) {
										mAdapter.setLstData(entity.getBody()
												.getData());
									} else {
										mAdapter.setAddLstData(entity.getBody()
												.getData());
									}
								}
								if ((mCurIndex + 1) < entity.getBody()
										.getPage()) {
									mCurIndex++;
									if (mRefreshListView != null) {
										mRefreshListView.setMode(Mode.BOTH);
									}
								} else {
									if (mRefreshListView != null) {
										mRefreshListView
												.setMode(Mode.PULL_FROM_START);
									}
								}
							} else {
								setEmptyView();
							}
						} else {
							setEmptyView();
						}
					}

					@Override
					public void onFailure(int code, String message) {
						// TODO Auto-generated method stub
						if (mRefreshListView != null) {
							mRefreshListView.onRefreshComplete();
						}
						setEmptyView();
						// showToast(message);
						Logger.i(TAG, message);
					}

				});
	}

}
